﻿////////////////////////////////////////////////////////////////////////////////////////////////////
//
//  © 2012 Stanislav S. Yarmonov.
//
//  TODO:LICENSE
//     
////////////////////////////////////////////////////////////////////////////////////////////////////
using System;

namespace Pandora
{
    ////////////////////////////////////////////////////////////////////////////////////////////////
    /// <summary>
    /// Extensions for the System.Type.
    /// </summary>
    ////////////////////////////////////////////////////////////////////////////////////////////////
    public static class TypeExtensions
    {
        ////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Activate the type instance.
        /// </summary>
        /// <param name="type">
        /// Type class extended.
        /// </param>
        /// <returns>
        /// Returns type instance.
        /// </returns>
        /// <project id="Yarmonov.pro">
        /// This method is similair to the generic one described below.
        /// </project>
        ////////////////////////////////////////////////////////////////////////////////////////////
        public static object Activate(this Type type)
        {
            return Activator.CreateInstance(type);
        }

        ////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Activate the type instance.
        /// </summary>
        /// <typeparam name="T">
        /// Type of the instance to be created.
        /// </typeparam>
        /// <param name="type">
        /// Type class extended.
        /// </param>
        /// <returns>
        /// Returns type instance.
        /// </returns>
        /// <project id="Yarmonov.pro">
        /// This method is directly used.
        /// </project>
        ////////////////////////////////////////////////////////////////////////////////////////////
        public static T Activate<T>(this Type type)
            where T : class
        {
            T result = Activator.CreateInstance(type) as T;

            if (result == null)
            {
                throw new InvalidCastException(); //TODO:More descriptive message.
            }

            return result;
        }
    }
}
